﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Manul.Genetics.Genom;
using Manul.Genetics.Character;
using System.Xml.Serialization;

namespace Manul.Genetics
{
    public class GenomData : NamedItem, IDisposable, ICompilable
    {
        public GenomDirectory GenomValues = new GenomDirectory(true);
        public CharacterDirectory CharacterValues = new CharacterDirectory(true);

        public GenomData()
        {
        }

        public void UpdateEpistasis()
        {
            List<CharacterItem> characterItems = new List<CharacterItem>();
            characterItems.AddRange(CharacterValues.Characters);
            int i=0;
            while (i < characterItems.Count)
            {
                if (characterItems[i] is CharacterDirectory)
                    characterItems.AddRange((characterItems[i] as CharacterDirectory).Characters);
                else if (characterItems[i] is Manul.Genetics.Character.Character)
                    (characterItems[i] as Manul.Genetics.Character.Character).LoadEpistasisByGuid(CharacterValues);
                i++;
            }
        }

        #region IDisposable Members

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }

        #endregion

        #region ICompilable Members

        public List<Manul.Genetics.Compiler.CompilationResult> Compile(GenomData genomDataItem)
        {
            List<Manul.Genetics.Compiler.CompilationResult> result = new List<Manul.Genetics.Compiler.CompilationResult>();
            foreach (Manul.Genetics.Character.Character character in this.CharacterValues.Characters)
                result.AddRange(character.Compile(this));
            return result;
        }

        #endregion
    }
}
